#include "CMinion.h"

#include "COverlord.h"
#include "core/loncore.h"

CMinion::CMinion()
{
  minion_id_ = 0;
  stats.level = 1;

  // Default State
  stats.name = L"Default";
  stats.hp = 50;
  stats.maxHp = 50;
  stats.mp = 10;
  stats.maxMp = 10;

  stats.atk = 10;    // Attack point
  stats.mAtk = 10;   // Magic attack
  stats.def = 5;    // Defend
  stats.mDef = 5;   // Magic Defend
  stats.speed = 5;  // Speed point (Delay)

  stats.movement = 100;
  stats.range = 50;
}

CMinion::CMinion(minionStats &stats) : stats(stats)
{
  if (stats.name.size() == 0) stats.name = stats.typeName;
  stringc temp = stats.name;
  minion_id_ = 0;
  cout << "CMinion of name: \"" << temp.c_str() << "\" has been created." << endl;
}

CMinion::~CMinion()
{
  stringc temp = stats.name;
  cout << "CMinion of name: \"" << temp.c_str() << "\" has been deleted." << endl;
}

//! Gets
minionStats& CMinion::getStats()
{
  return stats;
}

std::map <int, minionStats> CMinion::minion_base;
irr::core::map<stringw, ITexture*> CMinion::vMinionPic;
irr::core::map<stringw, stringc> CMinion::vMinionModel;

ITexture* CMinion::getMinionPic(stringw typeName)
{
  return vMinionPic[typeName];
}

stringc CMinion::getMinionModel(stringw typeName)
{
  return vMinionModel[typeName];
}

void CMinion::loadMinionBase(IVideoDriver *driver)
{
  // create the XML reader
  IXMLReader *xml = (IXMLReader*) createIrrXMLReaderUTF16("minions.xml");
  // file could not be opened - Default stats
  if (xml == 0)
  {
    std::cout << "Error: Could not load minions.xml" << std::endl;
    return;
  }
  int minion_type_id = 1;
  // parse the file until end reached
  while(xml->read())
  {
    // Check for minion node
    stringw node(xml->getNodeName());
    if ( node == L"minion" )
    {

      minionStats base_stat;

      base_stat.typeName = xml->getAttributeValue(L"name");
      base_stat.maxHp = xml->getAttributeValueAsInt(L"hp");
      base_stat.maxMp = xml->getAttributeValueAsInt(L"mp");

      base_stat.atk = xml->getAttributeValueAsInt(L"atk");    // Attack point
      base_stat.def = xml->getAttributeValueAsInt(L"def");    // Defend

      base_stat.mAtk = xml->getAttributeValueAsInt(L"matk");   // Magic attack
      base_stat.mDef = xml->getAttributeValueAsInt(L"mdef");   // Magic Defend

      base_stat.speed = xml->getAttributeValueAsInt(L"spd");  // Speed point (Delay)
      base_stat.range = xml->getAttributeValueAsFloat (L"range");
      base_stat.movement = xml->getAttributeValueAsFloat (L"move");

      base_stat.level = 1;

      minion_base[minion_type_id++] = base_stat;;

      if ( driver )
      {
        stringc picture = "minion\\";
        picture += xml->getAttributeValue(L"picture");
        vMinionPic[base_stat.typeName] = driver->getTexture(picture.c_str());
      }

      stringc model = "minion\\";

      if ( xml->getAttributeValue(L"model") == 0 )
        model = "NULL";
      else
        model += xml->getAttributeValue(L"model");

      vMinionModel[base_stat.typeName] = model;
    }
  }

  // delete the xml parser after usage

  xml->drop();

}



void CMinion::loadMinionBaseNew(bool loadGraphic)
{
  CDalMinion dal;
  CDalRecord &pResult = (*dal.getMinionType());

  // parse the file until end reached
  while(pResult.readNext())
  {
      minionStats base_stat;

      base_stat.typeName = pResult.getString("type_name");
      base_stat.maxHp = pResult.getInt("hp");
      base_stat.hp = base_stat.maxHp;

      base_stat.maxMp = pResult.getInt("mp");
      base_stat.mp = base_stat.maxMp;

      base_stat.atk = pResult.getInt("attack");    // Attack point
      base_stat.def = pResult.getInt("defense");    // Defend

      base_stat.mAtk = pResult.getInt("magic_attack");   // Magic attack
      base_stat.mDef = pResult.getInt("magic_defense");   // Magic Defend

      base_stat.speed = pResult.getInt("speed");  // Speed point (Delay)
      base_stat.range = pResult.getDouble("range");
      base_stat.movement = pResult.getDouble("movement");

      base_stat.level = 1;

      //minionBase.displayStats();

      int minion_type_id = pResult.getInt("type_id");

      minion_base[minion_type_id] = base_stat;
  }

  // drop
  delete &pResult;
}
